﻿using System;
using System.Net;
using System.ComponentModel;
using System.Collections.Generic;

/// <summary>
/// Summary description for MikeKappel
/// </summary>
namespace MikeKappel.Com.CS.Data.BindingLists
{
    /// <summary>
    /// Enum Binding List
    /// This call is an easy way to Databind to an Enum
    /// </summary>
    [Serializable]
    public class EnumBindingList : BindingList<EnumBindingList.EnumItem>
    {
        /// <summary>
        /// Bind to Enumeration
        /// </summary>
        /// <param name="enumType">type of Enumeration</param>
        public EnumBindingList(Type enumType):this (enumType, true)
        {
        }
        /// <summary>
        /// Bind to Enumeration
        /// </summary>
        /// <param name="enumType">type of Enumeration</param>
        /// <param name="useSpaces">Splits Camel Case Enums</param>
        public EnumBindingList(Type enumType, Boolean useSpaces)
            : this(enumType, useSpaces, null)
        {

        }
        /// <summary>
        /// Bind to Enumeration
        /// </summary>
        /// <param name="enumType">Type of Enumeration</param>
        /// <param name="exclusion">Enumerated Item to Exclude from Bind</param>
        public EnumBindingList(Type enumType, Object exclusion)
            : this(enumType, true, exclusion)
        {

        }
        /// <summary>
        /// Bind to Enumeration
        /// </summary>
        /// <param name="enumType">Type of Enumeration</param>
        /// <param name="useSpaces">Splits Camel Case Enums</param>
        /// <param name="exclusion">Enumerated Item to Exclude from Bind</param>
        public EnumBindingList(Type enumType, Boolean useSpaces, Object exclusion)
        {
            foreach (Object enumeratedItem in Enum.GetValues(enumType))
                if (exclusion == null || enumeratedItem != exclusion)
                    this.Add(new EnumItem(enumeratedItem, useSpaces));
        }
        /// <summary>
        /// Enumerated Item
        /// </summary>
        [Serializable]
        public class EnumItem
        {
            public EnumItem(Object enumeratedItem, Boolean useSpaces)
                :this(enumeratedItem.ToString(),(Int32)enumeratedItem,useSpaces)
            {

            }
            public EnumItem(String name, Int32 value)
                : this(name, value, true)
            {

            }
            public EnumItem(String name, Int32 value, Boolean useSpaces)
            {
                this.Name = (useSpaces) ? System.Text.RegularExpressions.Regex.Replace(name, "[A-Z]", " $0") : name;
                this.Value = value;
            }

            public String Name
            {
                get;
                private set;
            }
            public Int32 Value
            {
                get;
                private set;
            }
            public override string ToString()
            {
                return Name;
            }
        }
    }
}